﻿#include "OperVAOneXMLExport.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Kernel/FITKCore/FITKAbstractDataObject.h"
#include "FITK_Kernel/FITKCore/FITKThreadPool.h"
#include "FITK_Component/FITKVAOneXMLIO/FITKVAOneXMLIOInterface.h"
#include <QFileDialog>
#include "GUIFrame/MainWindow.h"

bool ModelOper::OperVAOneXMLExport::execGUI()
{
    //选择文件
    this->clearArgs();
    //主界面
    QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
    //获取工作目录
    QString wk;
    AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
    if (settings)
        wk = settings->getWorkingDir();

    QFileDialog dlg(mw, QObject::tr("Export XML File"), wk, "XML(*.xml)");
    dlg.setAcceptMode(QFileDialog::AcceptSave);  //保存模式
    dlg.show();
    //阻塞，否则键盘事件处理会出问题
    bool accept = false;
    QEventLoop loop;
    connect(&dlg, &QFileDialog::accepted, [&] {loop.quit(); accept = true;  });
    connect(&dlg, &QFileDialog::rejected, [&] {loop.quit(); accept = false;  });
    loop.exec();
    if (!accept) return false;
    QStringList files = dlg.selectedFiles(); //选择的文件名称
    if (files.isEmpty()) return false;
    QString fileName = files.at(0);
    //判断是否缺失扩展名
    QString fileExtension = ".xml";
    if (!fileName.contains(fileExtension)) fileName.append(fileExtension);

    if (fileName.isEmpty())
        return false;
    //存储信息
    if (_emitter == nullptr) return false;

    this->setArgs("FileName", fileName);
    return true;



}

bool ModelOper::OperVAOneXMLExport::execProfession()
{
    //导入文件名
    QString fileName;
    if (!this->argValue<QString>("FileName", fileName) || fileName.isEmpty()) return false;
    //读写组件获取
    VAOne::FITKVAOneXMLIOInterface* xmlIO = FITKAPP->getComponents()->
        getComponentTByName<VAOne::FITKVAOneXMLIOInterface>("VAOneXMLIO");

    //等待线程结束
    Core::FITKThreadPool::getInstance()->wait();

    if (xmlIO == nullptr) return false;

    GUI::MainWindow* mw = FITKAPP->getGlobalData()->getMainWindowT<GUI::MainWindow>();
    if (mw)
        mw->setProcessLabel(QString(tr("Exporting %1")).arg(fileName));

    //获取要存储的数据位置
    Core::FITKAbstractDataObject* model =
        FITKAPP->getGlobalData()->getMeshData<Core::FITKAbstractDataObject>();
    Core::FITKAbstractDataObject* physics =
        FITKAPP->getGlobalData()->getPhysicsData<Core::FITKAbstractDataObject>();
    //传入参数
    xmlIO->setDataObject("Model", model);
    xmlIO->setDataObject("Physics", physics);
    xmlIO->setFileName(fileName);
    xmlIO->runInThread();
    //关联结束导出槽函数
    connect(xmlIO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()), Qt::UniqueConnection);
    //执行导出
    xmlIO->exec(2);

    //保存执行的脚本
    QString scr = QString("Global.Files.ExportXMLFile('%1')").arg(fileName);
    this->saveScript(scr);

    return xmlIO->isSuccess();

}

void ModelOper::OperVAOneXMLExport::ioThreadFinishedSlot()
{
    VAOne::FITKVAOneXMLIOInterface* xmlIO = FITKAPP->getComponents()->
        getComponentTByName<VAOne::FITKVAOneXMLIOInterface>("VAOneXMLIO");
    if (xmlIO == nullptr) return;
    //取消关联
    disconnect(xmlIO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()));

}
